package com.mjk.common.tools.jna;


import com.mjk.common.util.ReflectionUtils;
import com.sun.jna.Library;

import java.util.HashMap;
import java.util.Map;

/**
 * 兩種使用方式
 * JNADllCall jna = context.getBean(JNADllCall.class);
 * //1
 * System.out.println(jna.invokeJniMethod(jna.getLibrary(SUNSONDll.class), "SUNSON_CloseDevice", new Class<?>[]{}, new Object[]{}));
 * //2
 * SUNSONDll sun = (SUNSONDll) jna.getLibrary(SUNSONDll.class);
 * System.out.println(sun.SUNSON_CloseDevice());
 *
 * @author mjk
 */
public class JNADllCall {

	private Map<String, Object> dllMap = new HashMap<String, Object>();

	public JNADllCall(Map<String, Object> libMap) {
		this.dllMap = libMap;
	}

	public Object getLibrary(Class<? extends Library> clazz) {
		return dllMap.get(clazz.getSimpleName());
	}

	/**
	 * 执行JNI方法
	 *
	 * @param libClazz
	 * @param methodName
	 * @param parameterTypes
	 * @param args
	 * @return
	 */
	public Object invokeJniMethod(Class<? extends Library> libClazz, String methodName,
								  Class<?>[] parameterTypes, Object[] args) {
		return ReflectionUtils.invoke(getLibrary(libClazz), methodName, parameterTypes, args);
	}
}
